import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material
import "components"
import "dialogs"
import "views"

ApplicationWindow {
    id: root
    visible: true
    width: 1600
    height: 900
    minimumWidth: 1280
    minimumHeight: 720
    title: qsTr("学生信息管理系统")
    color: "transparent"

    Material.theme: Material.Dark
    Material.accent: "#6a5acd"
    Material.primary: "#4b0082"

    // 背景渐变层
    Rectangle {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#0c0c1d" }
            GradientStop { position: 0.5; color: "#1d1d3a" }
            GradientStop { position: 1.0; color: "#2d2d5a" }
        }
    }

    // 背景装饰元素
    Repeater {
        model: 20
        delegate: Rectangle {
            width: 150
            height: 150
            radius: width/2
            color: Qt.rgba(0.4, 0.3, 0.8, 0.03)
            x: Math.random() * parent.width
            y: Math.random() * parent.height
        }
    }

    // 现代化标题栏
    header: Rectangle {
        height: 70
        radius: 0
        opacity: 0.9

        // 使用 Gradient 替代 LinearGradient
        gradient: Gradient {
            orientation: Gradient.Horizontal
            GradientStop { position: 0.0; color: Material.primary }
            GradientStop { position: 1.0; color: Qt.darker(Material.primary, 1.2) }
        }

        RowLayout {
            anchors.fill: parent
            spacing: 20

            Label {
                text: "📚 学生信息管理系统"
                font {
                    bold: true
                    pixelSize: 26
                    family: "Microsoft YaHei"
                }
                color: "white"
                Layout.leftMargin: 40
            }

            Item { Layout.fillWidth: true } // 占位

            // 用户区域
            Row {
                spacing: 15
                rightPadding: 20

                Rectangle {
                    width: 40
                    height: 40
                    radius: 20
                    color: "#5a4b9f"
                    Label {
                        text: "👤"
                        font.pixelSize: 20
                        anchors.centerIn: parent
                    }
                }

                Column {
                    anchors.verticalCenter: parent.verticalCenter
                    Label {
                        text: "管理员"
                        font.pixelSize: 14
                        color: "#e0e0e0"
                    }
                    Label {
                        text: "admin@edu.com"
                        font.pixelSize: 12
                        color: "#a0a0c0"
                    }
                }

                ToolButton {
                    text: "⚙️"
                    font.pixelSize: 18
                    onClicked: accountMenu.open()

                    background: Rectangle {
                        radius: 4
                        color: parent.hovered ? "#5a4b9f" : "transparent"
                    }

                    Menu {
                        id: accountMenu
                        MenuItem {
                            text: "🔄 刷新数据"
                            onTriggered: StudentService.loadData()
                        }
                        MenuItem { text: "⚙️ 系统设置" }
                        MenuItem { text: "🚪 退出系统"; onTriggered: Qt.quit() }
                    }
                }
            }
        }
    }

    // 主布局
    RowLayout {
        anchors.fill: parent
        spacing: 0
        anchors.margins: 20

        // 导航菜单
        NavigationMenu {
            id: navMenu
            Layout.preferredWidth: 300
            Layout.fillHeight: true
            // 修复信号处理函数 - 使用带参数的函数
            onMenuSelected: function(viewPath) {
                contentLoader.source = viewPath;
            }
        }

        // 分隔线
        Rectangle {
            width: 1
            Layout.fillHeight: true
            color: "#35354f"
        }

        // 内容区域
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: "transparent"
            radius: 12
            clip: true

            // 内容区域背景
            Rectangle {
                anchors.fill: parent
                color: "#1a1a2e"
                opacity: 0.8
                radius: parent.radius
            }

            // 加载器动画
            Loader {
                id: contentLoader
                anchors {
                    fill: parent
                    margins: 20
                }
                source: "qrc:/src/ui/views/ListView.qml"

                onStatusChanged: {
                    if (status === Loader.Loading) {
                        loadingIndicator.running = true;
                    } else {
                        loadingIndicator.running = false;
                    }
                }

                BusyIndicator {
                    id: loadingIndicator
                    anchors.centerIn: parent
                    running: false
                    Material.accent: Material.primary
                    width: 80
                    height: 80
                }
            }
        }
    }
}